package com.hy.demo.ui.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hy.demo.R;
import com.hy.demo.adapter.HomeAdapter;
import com.hy.demo.app.BaseFragment;
import com.hy.demo.model.MenuInfo;
import com.hy.demo.ui.MenuActivity;
import com.hy.demo.util.MenuUtil;
import com.hy.frame.adapter.IAdapterListener;
import com.hy.frame.recycler.GridItemDecoration;
import com.hy.frame.util.ActUtil;
import com.hy.frame.util.FormatUtil;
import com.hy.frame.util.ResUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

public class HomeFragment extends BaseFragment implements IAdapterListener<MenuInfo> {
    private RecyclerView rcyList;
    private List<MenuInfo> datas;
    private HomeAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.f_home;
    }

    @Override
    protected void initView() {
        setTitle("首页");
        rcyList = findViewById(R.id.home_rcyList);
        if (rcyList == null) return;
        rcyList.setLayoutManager(new GridLayoutManager(getContext(), 3));
        if (rcyList.getItemDecorationCount() == 0) {//防止界面恢复 重复添加
            int padding = ResUtil.dip2px(getContext(), 10);
            rcyList.addItemDecoration(new GridItemDecoration(padding).setVerticalDivider(padding).setPadding(padding, padding, padding, padding));
        }
    }

    @Override
    protected void initData() {
        datas = MenuUtil.getMenus(getContext(), R.xml.menu_main);
        updateUI();
    }

    private void updateUI() {
        if (adapter == null) {
            adapter = new HomeAdapter(getContext(), datas, this);
            rcyList.setAdapter(adapter);
        } else
            adapter.refresh(datas);
    }

    private int getXmlId(String name) {
        return getResources().getIdentifier(name, "xml", getContext().getPackageName());
    }

    @Override
    public void onViewClick(View v, MenuInfo item, int position) {
        String clsStr = item.getValue(MenuActivity.KEY_CLS);
        String menuStr = item.getValue(MenuActivity.KEY_MENU);
        if (FormatUtil.isNoEmpty(menuStr)) {
            int xmlId = getXmlId(menuStr);
            Bundle bundle = new Bundle();
            bundle.putInt(MenuActivity.ARG_XMLID, xmlId);
            bundle.putInt(MenuActivity.ARG_TITLEID, item.getTitle());
            ActUtil.startAct(this, MenuActivity.class, bundle);
            return;
        }
        if (FormatUtil.isEmpty(clsStr)) return;
        String args = item.getValue(MenuActivity.KEY_ARGS);
        Bundle bundle = new Bundle();
        if (FormatUtil.isNoEmpty(args)) {
            JSONArray json;
            try {
                json = new JSONArray(args);
                String type;
                String key;
                JSONObject obj;
                if (json.length() > 0) {
                    for (int i = 0; i < json.length(); i++) {
                        obj = (JSONObject) json.get(0);
                        type = obj.getString("type");
                        key = obj.getString("key");
                        if (type.equalsIgnoreCase("int")) {
                            bundle.putInt(key, obj.getInt("value"));
                        } else if (type.equalsIgnoreCase("long")) {
                            bundle.putLong(key, obj.getLong("value"));
                        } else if (type.equalsIgnoreCase("boolean")) {
                            bundle.putBoolean(key, obj.getBoolean("value"));
                        } else if (type.equalsIgnoreCase("double")) {
                            bundle.putDouble(key, obj.getDouble("value"));
                        } else {
                            bundle.putString(key, obj.getString("value"));
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            Class<?> cls = Class.forName(clsStr);
            ActUtil.startAct(this, cls, bundle);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static HomeFragment newInstance(@Nullable FragmentActivity act) {
        if (act != null) {
            String tag = HomeFragment.class.getSimpleName();
            Fragment fragment = act.getSupportFragmentManager().findFragmentByTag(tag);
            if (fragment instanceof HomeFragment)
                return (HomeFragment) fragment;
        }
        return new HomeFragment();
    }

}
